package com.shujia.filter;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.QualifierFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class Code04RowFilter {
    Connection connection;
    @Before
    public void getConnection(){
        Configuration configuration = new Configuration();

        // 注意：需要在当前平台中配置node1 node2的主机映射
        configuration.set("hbase.zookeeper.quorum","192.168.163.100,node1,node2");
        try {
            connection  = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Test
    public void getRow() throws IOException {
        Table table = connection.getTable(TableName.valueOf("api:stu"));

        Scan scan = new Scan();

        // 获取 所有ID以 0 结尾的数据

        // public RowFilter(final CompareOperator op,
        //                   final ByteArrayComparable rowComparator)
        RowFilter rowFilter = new RowFilter(
                CompareOperator.EQUAL,
                new RegexStringComparator(".*0$")
        );

        scan.setFilter(rowFilter);
        for (Result result : table.getScanner(scan)) {
            ResultUtil.printRes(result);
        }



    }


    @After
    public void close(){
        try {
            connection.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
